#!/bin/bash 
set -e


TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage4}
SRC=${SRC:-/sources}

name=nss
version=3.61

rm -rf $TMP
mkdir -p $TMP $LOG

tar xf $SRC/$name-$version.tar.gz -C $TMP

{ time \
   {

    cd $TMP/$name-$version
    
	patch -Np1 -i $SRC/$name-$version-standalone-1.patch

	cd nss

	make -j1 BUILD_OPT=1                  \
	  NSPR_INCLUDE_DIR=/usr/include/nspr  \
	  USE_SYSTEM_ZLIB=1                   \
	  ZLIB_LIBS=-lz                       \
	  NSS_ENABLE_WERROR=0                 \
	  $([ $(uname -m) = x86_64 ] && echo USE_64=1) \
	  $([ -f /usr/include/sqlite3.h ] && echo NSS_USE_SYSTEM_SQLITE=1)

	cd ../dist

	install -v -m755 Linux*/lib/*.so              /usr/lib
	install -v -m644 Linux*/lib/{*.chk,libcrmf.a} /usr/lib

	install -v -m755 -d                           /usr/include/nss
	cp -v -RL {public,private}/nss/*              /usr/include/nss
	chmod -v 644                                  /usr/include/nss/*

	install -v -m755 Linux*/bin/{certutil,nss-config,pk12util} /usr/bin

	install -v -m644 Linux*/lib/pkgconfig/nss.pc  /usr/lib/pkgconfig

    }
} 2>&1 | tee $name.log
    
[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
